import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:easy_refresh/easy_refresh.dart';

import '../../../models/banner.dart';
import '../../../widgets/load_image_widget.dart';
import 'logic.dart';

class ManageBannersPage extends StatelessWidget {
  ManageBannersPage({Key? key}) : super(key: key);

  final ManageBannersLogic logic = Get.put(ManageBannersLogic());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('管理轮播图'),
        backgroundColor: Colors.green,
        actions: [
          IconButton(
            icon: Icon(Icons.add),
            onPressed: logic.goToAddBanner,
          ),
        ],
      ),
      body: Obx(() {
        if (logic.isLoading.value) {
          return Center(child: CircularProgressIndicator());
        }
        
        if (logic.bannerList.isEmpty) {
          return Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Icon(Icons.image_not_supported_outlined, size: 60, color: Colors.grey),
                SizedBox(height: 16),
                Text(
                  '暂无轮播图',
                  style: TextStyle(
                    fontSize: 16.sp,
                    color: Colors.grey,
                  ),
                ),
              ],
            ),
          );
        }
        
        return EasyRefresh(
          onRefresh: logic.loadBanners,
          child: ListView.builder(
            padding: EdgeInsets.all(16.w),
            itemCount: logic.bannerList.length,
            itemBuilder: (context, index) {
              return _buildBannerCard(logic.bannerList[index]);
            },
          ),
        );
      }),
    );
  }

  Widget _buildBannerCard(BannerItem banner) {
    return Card(
      margin: EdgeInsets.only(bottom: 16.w),
      elevation: 2,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12.r),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          ClipRRect(
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(12.r),
              topRight: Radius.circular(12.r),
            ),
            child: banner.imageUrl.startsWith('/')
                ? Image.file(
                    File(banner.imageUrl),
                    height: 180.h,
                    width: double.infinity,
                    fit: BoxFit.cover,
                  )
                : LoadImageWidget(
                    banner.imageUrl,
                    height: 180.h,
                    width: double.infinity,
                  ),
          ),
          Padding(
            padding: EdgeInsets.all(16.w),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  banner.description,
                  style: TextStyle(
                    fontSize: 16.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                SizedBox(height: 8.h),
                Text(
                  banner.websiteUrl,
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: Colors.blue,
                  ),
                ),
                SizedBox(height: 16.h),
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    TextButton.icon(
                      onPressed: () => _showDeleteConfirmation(banner),
                      icon: Icon(Icons.delete, color: Colors.red),
                      label: Text('删除', style: TextStyle(color: Colors.red)),
                    ),
                  ],
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  void _showDeleteConfirmation(BannerItem banner) {
    Get.dialog(
      AlertDialog(
        title: Text('确认删除'),
        content: Text('确定要删除这个轮播图吗？'),
        actions: [
          TextButton(
            onPressed: () => Get.back(),
            child: Text('取消'),
          ),
          TextButton(
            onPressed: () {
              Get.back();
              logic.deleteBanner(banner.id!);
            },
            child: Text('删除', style: TextStyle(color: Colors.red)),
          ),
        ],
      ),
    );
  }
}
